package hku.cs.lunuo.launch;

import hku.cs.lunuo.LunuoPlugin;
import hku.cs.lunuo.page.PreferenceConstant;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class LunuoArgumentTab extends AbstractLaunchConfigurationTab {

	private ComboViewer algViewer;
	private final Image fLunuoIcon = createImage("sample.gif");

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		setControl(composite);
		GridLayout topLayout = new GridLayout();
		topLayout.numColumns = 1;
		composite.setLayout(topLayout);

		createAlgCombo(composite);
	}

	private void createAlgCombo(Composite comp) {
		Label algsLabel = new Label(comp, SWT.NONE);
		algsLabel.setText("Rank Algorithm");
		GridData gd = new GridData();
		gd.horizontalIndent = 0;
		algsLabel.setLayoutData(gd);

		algViewer = new ComboViewer(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		algViewer.getCombo().setLayoutData(
				new GridData(GridData.FILL_HORIZONTAL));

		algViewer.setContentProvider(new ArrayContentProvider());
		algViewer.setLabelProvider(new LabelProvider());
		algViewer.setInput(LunuoLaunchConstant.ALGS);
		algViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				updateLaunchConfigurationDialog();
			}
		});

	}

	private Image createImage(String path) {
		return LunuoPlugin.getDefault().getImageDescriptor(path).createImage();
	}

	@Override
	public String getName() {
		return LunuoLaunchConstant.TAB_NAME;
	}

	@Override
	public void initializeFrom(ILaunchConfiguration configuration) {
		String defaultAlg = LunuoPlugin.getDefault().getPreferenceStore()
				.getString(PreferenceConstant.ALG_KEY);
		try {
			String alg = configuration.getAttribute(
					LunuoLaunchConstant.ATT_RANK_NAME, LunuoLaunchConstant.ATT_NULL);
			if(alg.equals(LunuoLaunchConstant.ATT_NULL))
				algViewer.setSelection(new StructuredSelection(
					defaultAlg));
			else
				algViewer.setSelection(new StructuredSelection(alg));
		} catch (CoreException ce) {
			algViewer.setSelection(new StructuredSelection(defaultAlg));
		}

	}

	@Override
	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
		configuration.setAttribute(LunuoLaunchConstant.ATT_RANK_NAME,
				getSelectRankAlgName());

	}

	private String getSelectRankAlgName() {
		IStructuredSelection selection = (IStructuredSelection) algViewer
				.getSelection();
		return (String) selection.getFirstElement();
	}

	@Override
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
		String defaultAlg = LunuoPlugin.getDefault().getPreferenceStore()
			.getString(PreferenceConstant.ALG_KEY);
		configuration.setAttribute(LunuoLaunchConstant.ATT_RANK_NAME,
				defaultAlg);

	}

	public Image getImage() {
		return fLunuoIcon;
	}

	public void dispose() {
		super.dispose();
		fLunuoIcon.dispose();
	}

}
